package net.adetech.web.utils.spring;

import net.adetech.utils.UtilsBean;
import net.adetech.utils.spring.IEntity;
import net.adetech.web.utils.UtilsWeb;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.InitBinder;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.UUID;

/**
 * Created by sunfeihu on 2017/4/14.
 */
public abstract class BaseController {

    private final Logger logger = LoggerFactory.getLogger(BaseController.class);

    @InitBinder
    protected void initBinder(WebDataBinder binder) {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        binder.registerCustomEditor(java.util.Date.class, new CustomDateEditor(df, true));
        df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        binder.registerCustomEditor(java.util.Date.class, new CustomDateEditor(df, true));
    }

    @ExceptionHandler(Exception.class)
    protected void exceptionHandler(Exception ex, HttpServletResponse response, HttpServletRequest request) {
        ex.printStackTrace();
    }

    protected <T> T wrapper(Class<T> clzz, HttpServletRequest request) {
        T entity = UtilsBean.wrapper(clzz, request.getParameterMap());
        if (entity instanceof IEntity) {
            IEntity e = (IEntity) entity;
            if (null == e.getId() || e.getId().equals(0L)) {
                e.setAddDate(Calendar.getInstance().getTime());
                e.setUuid(UUID.randomUUID().toString());
                e.setAddIp(UtilsWeb.getIp(request));
                e.setAddUser("");
                e.setIsDelete(false);
            }
            e.setUpdateDate(Calendar.getInstance().getTime());
            e.setUpdateIp(UtilsWeb.getIp(request));
            e.setAddUser("");
        }
        return entity;
    }

    /**
     * @param clzz
     * @param request
     * @param prefix  参数前缀
     * @param <T>
     * @return
     */
    protected <T> T wrapper(Class<T> clzz, HttpServletRequest request, String prefix) {
        T entity = UtilsBean.wrapper(clzz, request.getParameterMap(), prefix);
        if (entity instanceof IEntity) {
            IEntity e = (IEntity) entity;
            if (null == e.getId() || e.getId().equals(0L)) {
                e.setAddDate(Calendar.getInstance().getTime());
                e.setUuid(UUID.randomUUID().toString());
                e.setAddIp(UtilsWeb.getIp(request));
                e.setAddUser("");
                e.setIsDelete(false);
            }
            e.setUpdateDate(Calendar.getInstance().getTime());
            e.setUpdateIp(UtilsWeb.getIp(request));
            e.setAddUser("");
        }
        return entity;
    }

    /**
     * 组织loginfo
     *
     * @param request
     * @param logger  logger
     * @param operate 操作
     * @param content 操作详细
     */
    protected void opLog(HttpServletRequest request, Logger logger, String operate, String content) {
        //"用户名，操作，操作详细，IP"
        logger.info("EBMS_INFO:{},{},{},{}", "", operate, content, UtilsWeb.getIp(request));
    }


}